package com.microexam.sunproduct.controller;

import com.microexam.suncommon.domain.Product;
import com.microexam.suncommon.util.Constants;
import com.microexam.suncommon.util.ResultVO;
import com.microexam.sunproduct.service.ProductService;
import feign.Param;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author machuang
 * @create 2020/04/2020/4/29
 */
@Controller
@RequestMapping("")
public class ProductController {
    @Autowired
    ProductService service;

    @GetMapping("/getProduct/{id}")
    @ResponseBody
    @GlobalTransactional
    public ResultVO  getOne  (@PathVariable int id) {
        int i=1;
        System.out.println("find=========find"+id);
        ResultVO resultVO = null;
        try {
            Product product = service.queryById(id);
            resultVO = new ResultVO(Constants.OPER_SUCCESS, "test ok", product);
        } catch (Exception e) {
            resultVO = new ResultVO(Constants.OPER_FAILURE, "查询不到该商品");
        }
        return resultVO;
    }
    @GetMapping("/deductStock/{pid}/{number}")
    @ResponseBody
    @GlobalTransactional
    public ResultVO deductStock(@PathVariable Integer pid, @PathVariable Integer number) {
        System.out.println(pid+"number"+number);
        try {

            service.deductStock(pid,number);
            return new ResultVO(Constants.OPER_SUCCESS, "扣减库存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(Constants.OPER_SUCCESS, "扣减库存失败");

        }
    }

    @GetMapping("/products/testloadbalance")
    public ResultVO testLoadBalance(HttpServletRequest request) {
        return new ResultVO(Constants.OPER_SUCCESS, "处理本次请求的product-service实例端口：" + request.getServerPort());
    }
}
